package cn.pzhu.pserson.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.pzhu.pserson.util.ResponseUtil;
import cn.pzhu.pserson.model.Lipin;
import cn.pzhu.pserson.dto.LipinDTO;
import cn.pzhu.pserson.dto.LimitShowDTO;
import cn.pzhu.pserson.service.ILipinService;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@Controller
@RequestMapping("/gift")
public class LipinController {

    @Autowired
    private ILipinService lipinService;

    @CrossOrigin
    @RequestMapping(value = "/getRatio", method = RequestMethod.POST, produces = {"text/plain;charset=utf-8","text/html;charset=utf-8"}             )
    @ResponseBody
    public ResponseEntity<String> info(
            @Validated
                    LimitShowDTO limitShowDTO, BindingResult errors, HttpServletResponse response, HttpServletRequest request) {

        Map<String, Object> result = new HashMap<String, Object>();

//        List<Map<String, Object>> list = lipinService.loadMaps("surnum>?", new Integer[]{0}, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());


        List<Map<String, Object>> list = lipinService.getDataByDateTime();

        for(int i = 0;i<list.size();i++){

            list.get(i).put("levelname", getLevelTitle(Integer.parseInt(list.get(i).get("level") + "")));
        }

        result.put("ratio", list);

        return ResponseUtil.getResEntityForGetAndJson(result);
    }

    @RequestMapping(value = "/page/list", method = RequestMethod.GET)
    public ModelAndView list(Model model,
                                       @Validated
        LimitShowDTO limitShowDTO, BindingResult errors, HttpSession session) {

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(limitShowDTO.getPageNum());
        pageInfo.setPageSize(limitShowDTO.getPageSize());
        List<Map<String, Object>> list = lipinService.loadMaps(null, null, null, null, limitShowDTO.getPageNum(), limitShowDTO.getPageSize());
        double count = lipinService.count();

        for(int i = 0;i<list.size();i++){
            list.get(i).put("levelname", getLevelTitle(Integer.parseInt(list.get(i).get("level") + "")));
        }

        pageInfo.setList(list);
        pageInfo.setPages((int)Math.ceil(count/6));
        model.addAttribute("list", pageInfo.getList());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userid",session.getAttribute("userid"));
        return new ModelAndView("/lipin/list", (Map<String, ?>) model) ;
    }

    private String getLevelTitle (Integer param) {
        String result = "";
        switch (param){
            case 0:
                result =  "特等奖";
                break;
            case 1:
                result =  "一等奖";
                break;
            case 2:
                result =  "二等奖";
                break;
            case 3:
                result =  "三等奖";
                break;
            case 4:
                result =  "幸运奖";
                break;
        }

        return result;
    }

    @RequestMapping(value = "/page/add", method = RequestMethod.GET)
    public ModelAndView addshow() {

        return new ModelAndView("/lipin/add");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<String> show(@PathVariable("id") Integer id) {
        Map<String, Object> lipinModel = lipinService.findMap(id);

        return ResponseUtil.getResEntityForGetAndJson(lipinModel);
    }

    @RequestMapping(value = "/page/update/{id}", method = RequestMethod.GET)
    public ModelAndView updateshow(Model model,@PathVariable("id") Integer id) {

        model.addAttribute("info", lipinService.findMap(id));
        return new ModelAndView("/lipin/update", (Map<String, ?>) model);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseEntity<String> create(
        @Validated
        LipinDTO lipinDTO, BindingResult errors) {
        if(errors.hasErrors())
            return ResponseUtil.getResponseEntityWhenInvalidReqParams();

        Lipin lipinModel = lipinDTO.toModel();

        return ResponseUtil.getResEntityForPPP(lipinService.save(lipinModel));
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> update(@PathVariable("id") Integer id,
        @Validated
        LipinDTO lipinDTO, BindingResult errors) {

        Lipin lipinModel = lipinDTO.toModel();
        lipinModel.setId(id);
        return ResponseUtil.getResEntityForPPP(lipinService.update(lipinModel));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> destory(@PathVariable("id") Integer id) {
        return ResponseUtil.getResEntityForDel(lipinService.delete(id));
    }

    @RequestMapping(value = "/deletes", method = RequestMethod.DELETE)
    public ResponseEntity<String> deletes(@RequestParam("primaryKeys") Integer[] primaryKeys) {
        return ResponseUtil.getResEntityForDel(lipinService.batchDelete(primaryKeys));
    }
}
